/*
 * @(#) PartInterService.java 2013-8-6 下午1:22:14
 *
 * Copyright 2013 Rockwell Automation, Inc. All rights reserved.
 * Rockwell Automation PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
 */package com.osi.mes.inter.erp;

import java.util.ArrayList;
import java.util.Hashtable;
import java.util.Vector;

import pnuts.lang.Context;

import com.datasweep.compatibility.client.ATHandler;
import com.datasweep.compatibility.client.ATRow;
import com.datasweep.compatibility.client.ATRowFilter;
import com.datasweep.compatibility.client.BillOfMaterials;
import com.datasweep.compatibility.client.BomItem;
import com.datasweep.compatibility.client.DependentATRow;
import com.datasweep.compatibility.client.ParentATRow;
import com.datasweep.compatibility.client.Part;
import com.datasweep.compatibility.client.PartFilter;
import com.datasweep.compatibility.client.Response;
import com.datasweep.compatibility.pnuts.Functions;
import com.osi.mes.inter.BaseInterService;
import com.rockwell.ftpc.framework.common.XMLUtils;
import com.rockwell.ftpc.framework.common.XmlData;
import com.rockwell.mes.rtmis.middle.constants.IMDConstants;

public class MatSplitMethodInterService extends BaseInterService
{

	private static MatSplitMethodInterService instatnce = null;

	private MatSplitMethodInterService(Context context)
	{
		super(context);
	}

	private MatSplitMethodInterService(Functions functions)
	{
		super(functions);
	}

	public static MatSplitMethodInterService getInstance(Context context)
	{
		if (instatnce == null)
		{
			synchronized (MatSplitMethodInterService.class)
			{
				if (instatnce == null)
				{
					instatnce = new MatSplitMethodInterService(context);
				}
			}
		}
		return instatnce;
	}

	public Hashtable<String, Object> receiveMatSplitMethod(Hashtable<String, Object> receivedData)
	{
		Hashtable<String, Object> returnValue = new Hashtable<String, Object>();
		try
		{
			String msgObjType = (String) receivedData.get(IMDConstants.MD_TABLE_COULMN_MSGObJ_TYPE);
			if (!"100004".equals(msgObjType))
			{
				throw new Exception(
					"The MSGObjType value does not matched, it should be 66 instead of "
						+ msgObjType);
			}
			String msgTransType = (String) receivedData
				.get(IMDConstants.MD_TABLE_COULMN_MSG_TRANS_TYPE);

			String msgKeyFields = (String) receivedData
				.get(IMDConstants.MD_TABLE_COULMN_MSG_KEY_FIELDS);
			String msgFieldsValue = (String) receivedData
				.get(IMDConstants.MD_TABLE_COULMN_MSG_FIELDS_VALUE);
			String msgContent = (String) receivedData.get(IMDConstants.MD_TABLE_COULMN_MSG_CONTENT);
			XmlData xmlData = XMLUtils.strToXMLData(msgContent);
			Hashtable<String, ArrayList<Object>> value = xmlData.getData();

			ATRowFilter atRowFilter = getFunctions().createATRowFilter(
				AT_TABLE_PM_MATERIAL_SPLIT_METHOD);
			atRowFilter.forColumnNameEqualTo(
				AT_COLUMN_PM_MATERIAL_SPLIT_METHOD_DOCENTRY, msgFieldsValue);
			ATHandler atHandler = getFunctions().createATHandler(
				AT_TABLE_PM_MATERIAL_SPLIT_METHOD);
			if (INTER_STR_CREATE.equalsIgnoreCase(msgTransType))
			{
				int atRowCount = getFunctions().getFilteredATRowCount(
					atRowFilter);
				if (atRowCount > 0)
				{
					throw new Exception(
						"The MatSplitMethod object has already existed in RTMIS, the DocEntry is "
							+ msgFieldsValue);
				}
				String partNumber = getTransferValue(
					value, "ItemCode");
				Part part = getFunctions().getPart(
					partNumber, "1");
				if (part == null)
				{
					throw new Exception("The Part object did not exist, ItemCode is " + partNumber);
				}
				ParentATRow atRow = (ParentATRow) atHandler.createATRow();
				atRow.setValue(
					AT_COLUMN_PM_MATERIAL_SPLIT_METHOD_DEST_PARTNUMBER, getTransferValue(
						value, "ItemCode"));
				atRow.setValue(
					AT_COLUMN_PM_MATERIAL_SPLIT_METHOD_SPLIT_METHOD, getTransferValue(
						value, "SplitMthd"));
				atRow.setValue(
					UDA_INTER_FLAG_SAP, INTER_CREATE);
				ArrayList<Object> lines = (ArrayList<Object>) value.get("Lines");
				for (int i = 0; i < lines.size(); i++)
				{
					Hashtable<String, ArrayList<Object>> line = (Hashtable<String, ArrayList<Object>>) lines
						.get(i);
					partNumber = getTransferValue(
						line, "ItemCode");
					part = getFunctions().getPart(
						partNumber, "1");
					if (part == null)
					{
						throw new Exception("The Part object did not exist, ItemCode is "
							+ partNumber);
					}
					DependentATRow dependATRow = atRow.createDependentATRow_(getFunctions()
						.getATDefinition(
							AT_TABLE_PM_MATERIAL_SPLIT_METHOD_MATERIALS),
						getFunctions().getDBTime().toString());
					dependATRow.setValue(
						AT_COLUMN_PM_MATERIAL_SPLIT_METHOD_MATERIALS_SOURCE_PARTNUMBER, partNumber);
				}
				Response response = atHandler.save();
				if (response.isError())
				{
					throw new Exception(response.getFirstErrorMessage());
				}
			}
			else if (INTER_STR_UPDATE.equalsIgnoreCase(msgTransType))
			{
				Vector atRows = atHandler.getATRowsByFilter(
					atRowFilter, false);
				if (atRows == null || atRows.size() == 0)
				{
					throw new Exception(
						"The MatSplitMethod object did not exist in RTMIS, the DocEntry is "
							+ msgFieldsValue);
				}
				String partNumber = getTransferValue(
					value, "ItemCode");
				Part part = getFunctions().getPart(
					partNumber, "1");
				if (part == null)
				{
					throw new Exception("The Part object did not exist, ItemCode is " + partNumber);
				}
				ParentATRow atRow = (ParentATRow) atRows.get(0);
				atRow.setValue(
					AT_COLUMN_PM_MATERIAL_SPLIT_METHOD_DEST_PARTNUMBER, getTransferValue(
						value, "ItemCode"));
				atRow.setValue(
					AT_COLUMN_PM_MATERIAL_SPLIT_METHOD_SPLIT_METHOD, getTransferValue(
						value, "SplitMthd"));
				atRow.setValue(
					UDA_INTER_FLAG_SAP, INTER_UPDATE);
				Vector oldDependentATRows = atRow.getDependentATRows();
				for (int i = 0; i < oldDependentATRows.size(); i++)
				{
					DependentATRow oldDependentATRow = (DependentATRow) oldDependentATRows.get(i);
					atRow.removeDependentATRow(oldDependentATRow);
				}
				ArrayList<Object> lines = (ArrayList<Object>) value.get("Lines");
				for (int i = 0; i < lines.size(); i++)
				{
					Hashtable<String, ArrayList<Object>> line = (Hashtable<String, ArrayList<Object>>) lines
						.get(i);
					partNumber = getTransferValue(
						line, "ItemCode");
					part = getFunctions().getPart(
						partNumber, "1");
					if (part == null)
					{
						throw new Exception("The Part object did not exist, ItemCode is "
							+ partNumber);
					}
					DependentATRow dependATRow = atRow.createDependentATRow_(getFunctions()
						.getATDefinition(
							AT_TABLE_PM_MATERIAL_SPLIT_METHOD_MATERIALS),
						getFunctions().getDBTime().toString());
					dependATRow.setValue(
						AT_COLUMN_PM_MATERIAL_SPLIT_METHOD_MATERIALS_SOURCE_PARTNUMBER, partNumber);
				}
				Response response = atHandler.save();
				if (response.isError())
				{
					throw new Exception(response.getFirstErrorMessage());
				}

			}
			else if (INTER_STR_DELETE.equalsIgnoreCase(msgTransType))
			{
				Vector atRows = atHandler.getATRowsByFilter(
					atRowFilter, false);
				if (atRows == null || atRows.size() == 0)
				{
					throw new Exception(
						"The MatSplitMethod object did not exist in RTMIS, the DocEntry is "
							+ msgFieldsValue);
				}
				ATRow atRow = (ATRow) atRows.get(0);
				// atRow.setValue(UDA_INTER_FLAG_SAP, INTER_DELETE);
				Response response = atRow.delete(
					getFunctions().getDBTime(), null, null);
				if (response.isError())
				{
					throw new Exception(response.getFirstErrorMessage());
				}
			}
			else
			{
				throw new Exception("The operation type is error.");
			}

			returnValue.put(
				IMDConstants.RESPONSE_STATUS, IMDConstants.RESPONSE_STATUS_SUCCESSFUL);
		}
		catch (Exception e)
		{
			returnValue.put(
				IMDConstants.RESPONSE_STATUS, IMDConstants.RESPONSE_STATUS_FAILED);
			returnValue.put(
				IMDConstants.RESPONSE_LAST_ERR_MSG, e.getMessage());
		}
		return returnValue;
	}
}
